import json
import os
import sys

import yaml
from main import Environ

class ReadYaml():
    def __init__(self,filename=''):
        #切换环境变量
        if filename!='':
            self.filepath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)) + Environ)+"/"+filename#拼接定位到data文件夹

    def get_yaml_data(self, full_path_file=''):
        if full_path_file != '':
            file = full_path_file
        else:
            file = self.filepath
        with open(file, "r", encoding="utf-8")as f:
            # 调用load方法加载文件流
            return yaml.load(f,Loader=yaml.FullLoader)

class ReadYaml_anjiekou():
    def __init__(self,filename):
        self.filepath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)) + r"/data")+"/"+filename#拼接定位到data文件夹

    def get_yaml_data(self):
        with open(self.filepath, "r", encoding="utf-8")as f:
            # 调用load方法加载文件流
            return yaml.load(f,Loader=yaml.FullLoader)

class ReadYaml_By_Root_Dict():
    def __init__(self,filename):
        self.filepath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)) )+"/"+filename#拼接定位到data文件夹

    def get_yaml_data(self):
        with open(self.filepath, "r", encoding="utf-8")as f:
            # 调用load方法加载文件流
            return yaml.load(f,Loader=yaml.FullLoader)

if __name__ == '__main__':
    data = ReadYaml_By_Root_Dict("configs/environment_var.yml").get_yaml_data()  # 读取数据
    print(json.dumps(data,indent=4))


